<template>
	<view>
		<!-- 自定义导航 -->
		<uni-nav-bar
			left-icon="back"
			@clickLeft="backTo()"
			backgroundColor="#ffefd6"
			:border="false" statusBar
		></uni-nav-bar>
		<view class="title">
			<view>你好，</view>
			<view>欢迎来到xx小说</view>
		</view>
		<view class="tologin">
			<view class="message">
				<!-- 手机号 -->
				<view>
					<view>手机号</view>
					<input
						type="number"
						v-model="phone"
						maxlength="11"
						placeholder="请输入手机号"
						placeholder-style="font-size:28rpx"
					/>
				</view>
				<!-- 密码 -->
				<view>
					<view>密码</view>
					<!-- <input type="password" value="" placeholder="请输入密码" /> -->
					<input
						maxlength="15"
						class="uni-input"
						v-model="upwd"
						password
						type="text"
						placeholder-style="font-size:28rpx"
						placeholder="请输入密码"
					/>
					<!-- <view :style="{color:huoqu?'#ff604f':'#999'}" >获取验证码</view> -->
				</view>
			</view>
			<view class="btn-login" @click="toLogin()">登录</view>
			<!-- 注册 -->
			<view
				style="display: flex;justify-content: flex-end;margin: 20rpx 10rpx;font-size: 32rpx;"
				@click="toRegist()"
			>
				没有账号？去注册
			</view>
			<!-- 第三方登录 -->
			<view class="three">
				<view>
					<view class="line"></view>
					<view>第三方账号快速登录</view>
					<view class="line"></view>
				</view>
				<view>
					<image src="../../static/weixin.png" style="width: 100rpx;" mode="widthFix"></image>
					<image src="../../static/weibo.png" style="width: 100rpx;" mode="widthFix"></image>
					<image src="../../static/QQ.png" style="width: 100rpx;" mode="widthFix"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			phone: '',
			upwd: ''
		};
	},
	methods: {
		// 返回上一页
		backTo() {
			uni.navigateBack({
				delta: 1
			});
		},
		// 跳转注册页面
		toRegist() {
			uni.navigateTo({
				url: '../regist/regist'
			});
		},
		toLogin() {
			if (this.phone.trim() == '' || this.upwd.trim() == '') return;

			uni.request({
				url: `${getApp().globalData.baseURL}/v1/user/login`,
				data: {
					phone: this.phone.trim(),
					upwd: this.upwd.trim()
				},
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded' // application/x-www-form-urlencoded
				},
				success: res => {
					console.log(res);
					if (res.data.code == 201) {
						uni.showToast({
							title: '用户名或密码错误，请重新输入',
							icon: 'none',
							duration: 1000
						});
						this.upwd = '';
					} else if (res.data.code == 200) {
						let user = res.data.data[0];
						// 登陆状态修改为true
						this.$store.commit('toLogin', true);
						// 存全局+本地 将数据存储在本地缓存中
						this.$store.commit('updateUser', user);
						// console.log(user)
						// console.log(this.$store.state.user);
						// console.log(this.$store.state.isLogin)
						// 返回之前页面 上一页
						uni.navigateBack({
							delta: 1
						});
						// // 跳转到tab页我的
						// uni.switchTab({
						// 	url: '../me/me'
						// });
					}
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.title {
	padding: 40rpx 40rpx 100rpx;
	font-size: 44rpx;
	background-color: #ffefd6;
	> view {
		margin-top: 20rpx;
	}
}
.tologin {
	margin-top: -30rpx;
	padding: 40rpx;
	background-color: #fff;
	border-radius: 40rpx;
	.message {
		width: 100%;
		padding-top: 30rpx;
		> view {
			padding: 30rpx 10rpx;
			display: flex;
			justify-content: space-between;
			flex: 1;
			border-bottom: 1px solid #999;
			> view:first-child {
				width: 140rpx;
				font-size: 32rpx;
			}
			> input {
				width: 520rpx;
			}
		}
	}
}
.btn-login {
	padding: 20rpx 16rpx;
	margin-top: 80rpx;
	border-radius: 44rpx;
	text-align: center;
	background-color: #ff604f;
	color: #fff;
	font-size: 38rpx;
	letter-spacing: 30rpx;
}
// 第三方登录
.three {
	margin-top: 100rpx;
	> view:first-child {
		padding: 30rpx 0;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.line {
			width: 160rpx;
			height: 0.5px;
			background-color: #ccc;
		}
	}
	> view:last-child {
		padding: 30rpx;
		display: flex;
		justify-content: space-around;
	}
}
</style>
